home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1999 May: Tool Chest / Developer CD Series Tool Chest (Apple Computer)(May 1999).iso / Tool Chest / Development Kits / MPW etc / MPW-GM / MPW / Scripts / MultiSearch < prev    next >
Encoding:
Text File  |  1998-11-23  |  7.5 KB  |  279 lines  |  [TEXT/MPS ]

  1. # MultiSearch
  2. #
  3. # Searches text files in a given directory tree for specified words,
  4. # phrases, and patterns.  A report is produced in one of Three forms.
  5. # The first two forms produce MPW command lines which may be executed 
  6. # to locate the offending passage in the document.  One of these forms
  7. # displays the matching text line as a comment after the MPW commands
  8. # while the other displays the matching text on a seperate line. The
  9. # third form just lists the lines containing the offending text.
  10. #
  11. # The words and patterns may be specified on the command line or in a
  12. # text file.
  13. #
  14. # Copyright © 1994-1995, 1998 Apple Computer Inc.
  15. #
  16. # Usage:
  17. #
  18. #    MultiSearch {-t target_dir | -j target_dir} {-f path | pattern…} ∂
  19. #                [-q | -v] [-p] [-i | -s] [-rp | -rf] 
  20. #
  21. # Where:
  22. #
  23. #    -t target_dir    specifies the path to the base directory to be recursively
  24. #                    searched.
  25. #    -j target_dir    specifies the path to the directory to be searched.  Only
  26. #                    the specified directory will be searched.
  27. #    -f path            specifies the path to a text file containing the
  28. #                    patterns, words, or phrases to search for.  Patterns
  29. #                    may not be specified on the command line if this option
  30. #                    is used. The file should contain a space or line
  31. #                    seperated list of patterns and string literals.  Note
  32. #                    that normal MPW quoting and pattern matching rules apply,
  33. #                    for example:
  34. #
  35. #                    fred joe
  36. #                    /merde≈/
  37. #
  38. #                    contains three patterns, 'fred', 'joe', and '/merde≈/'
  39. #                     while the following:
  40. #
  41. #                    'fred joe'
  42. #                    /merde≈/
  43. #
  44. #                    contains two patterns, 'fred joe' and /merde≈/
  45. #    pattern…        is a list of one or more patterns or string literals
  46. #                    to search for.  Patterns may not be specified on the
  47. #                    command line if the -f option is used.
  48. #    -q                specifies that the report should contain just the matched
  49. #                    text.  By default, MPW commands are generated with the
  50. #                    matched text appended as a comment.  The -q option may
  51. #                    not be specified with the -v option.
  52. #    -v                specifies that a verbose report should be generated.
  53. #                    The matched text is shown on one line and MPW commands
  54. #                    to locate it are given on a seperate line.  The -v option
  55. #                    may not be specified with the -q option.
  56. #    -p                Print progress and copyright information.  Included in
  57. #                    the progress report is information on which files do not
  58. #                    contain the search patterns
  59. #    -i                specifies that all matches are to be case-insensitive.
  60. #                    by default, case sensitivity is determined by the value
  61. #                    of the {CaseSensitive} shell variable.  This option may
  62. #                    not be used with -s.
  63. #    -s                specifies that all matches are to be case-sensitive.
  64. #                    by default, case sensitivity is determined by the value
  65. #                    of the {CaseSensitive} shell variable.  This option may
  66. #                    not be used with -i.
  67. #    -rp                report grouped by patterns
  68. #    -rf                report grouped by file (default)
  69. #    
  70.  
  71. set echo 0                        # We don't need to echo the contents of this script
  72.                                 # even if someone else is echoing commands
  73. set exit 0
  74.  
  75. set Patterns ""
  76. set searchOpts ""
  77. set targetDir ""
  78. set recursiveFiles "-r"
  79.  
  80. set verbose 0
  81. set quiet 0
  82. set sOpt 0
  83. set iOpt 0
  84. set Progress 0
  85. set byPattern 0
  86.  
  87. #
  88. # Parse the command line
  89. #
  90.  
  91. set usageError 0
  92. loop
  93.     break if "{#}" < 1
  94.     If "{{1}}" =~ /-[Ff]/
  95.         #
  96.         # -f pattern_list_file
  97.         #
  98.         if "{Patterns}" == ""
  99.             set Patterns "`catenate "{2}"`"
  100.             shift
  101.         else
  102.             echo "### {0} - Only one -f option may be specified...or..." >> Dev:StdErr
  103.             echo "###    Patterns may not be specified on the command line if -f is used." >> Dev:StdErr
  104.             set usageError 1
  105.         end
  106.     Else If "{{1}}" =~ /-[Tt]/
  107.         #
  108.         # -t target_dir option
  109.         #
  110.         if "{targetDir}" == ""
  111.             if "{2}" !~ /-≈/
  112.                 if "{2}" =~ /≈:/
  113.                     set targetDir "{2}"
  114.                 else if "{2}" =~ /≈:≈/
  115.                     set targetDir "{2}:"
  116.                 else
  117.                     set targetDir ":{2}:"
  118.                 end
  119.                 shift
  120.             else
  121.                 echo "### {0} - No target directory specified with -t option!" >> Dev:StdErr
  122.                 set usageError 1
  123.             end
  124.         else
  125.             echo "### {0} - Too many targets; Only one target directory or volume at a time, please!" >> Dev:StdErr
  126.             set usageError 1
  127.         end
  128.     Else If "{{1}}" =~ /-[Jj]/
  129.         #
  130.         # -j target_dir option
  131.         #
  132.         if "{targetDir}" == ""
  133.             if "{2}" !~ /-≈/
  134.                 if "{2}" =~ /≈:/
  135.                     set targetDir "{2}"
  136.                 else
  137.                     set targetDir "{2}:"
  138.                 end
  139.                 unset recursiveFiles
  140.                 shift
  141.             else
  142.                 echo "### {0} - No target directory specified with -j option!" >> Dev:StdErr
  143.                 set usageError 1
  144.             end
  145.         else
  146.             echo "### {0} - Too many targets; Only one target directory or volume at a time, please!" >> Dev:StdErr
  147.             set usageError 1
  148.         end
  149.     Else if "{{1}}" =~ /-[Qq]/
  150.         if "{verbose}"
  151.             echo "### {0} - The -q option may not be used with the -v option" >> Dev:StdErr
  152.             set usageError 1
  153.         else if !"{quiet}"
  154.             set quiet 1
  155.             set searchOpts "{SearchOpts} -q"
  156.         end
  157.     Else if "{{1}}" =~ /-[Vv]/
  158.         if "{quiet}"
  159.             echo "### {0} - The -q option may not be used with the -v option" >> Dev:StdErr
  160.             set usageError 1
  161.         else if !"{verbose}"
  162.             set verbose 1
  163.             set searchOpts "{SearchOpts} -b"
  164.         end
  165.     Else if "{{1}}" =~ /-[Pp]/
  166.         set progress 1
  167.     Else if "{{1}}" =~ /-[Rr][Pp]/
  168.         set byPattern 1
  169.     Else if "{{1}}" =~ /-[Rr][Ff]/
  170.         set byPattern 0
  171.     Else if "{{1}}" =~ /-[Ii]/
  172.         if "{sOpt}"
  173.             echo "### {0} - The -s option may not be used with the -i option" >> Dev:StdErr
  174.             set usageError 1
  175.         else if !"{quiet}"
  176.             set iOpt 1
  177.             set searchOpts "{SearchOpts} -i"
  178.         end
  179.     Else if "{{1}}" =~ /-[Ss]/
  180.         if "{iOpt}"
  181.             echo "### {0} - The -s option may not be used with the -i option" >> Dev:StdErr
  182.             set usageError 1
  183.         else if !"{quiet}"
  184.             set sOpt 1
  185.             set searchOpts "{SearchOpts} -s"
  186.         end
  187.     Else if "{{1}}" =~ /-≈/
  188.         #
  189.         # Unrecognized option
  190.         #
  191.         echo "### {0} - Unknown option ∂"{1}∂"" >> Dev:StdErr
  192.         set usageError 1
  193.     Else
  194.         set Patterns "{{Patterns}} {{1}}"
  195.     End
  196.     shift
  197. End
  198.  
  199. #
  200. # Verify that required options were specified
  201. #
  202.  
  203. if ! "{targetDir}"
  204.     echo "### {0} - a target directory must be specified using either -j or -t!" >> Dev:StdErr
  205.     set UsageError 1
  206. end
  207.  
  208. if ! "{{Patterns}}"
  209.     echo "### {0} - At least one pattern must be specified" >> Dev:StdErr
  210.     echo "#       either in a pattern file or on the command line" >> Dev:StdErr
  211.     set UsageError 1
  212. end
  213.  
  214. #
  215. # Check for usage errors
  216. #
  217.  
  218. if "{usageError}"
  219.     echo "# Usage:  {0} ∂{-t target_dir [-mount zone:server] | -j target_dir∂} ∂∂" >> Dev:StdErr
  220.     echo "#             ∂{-f path | pattern…∂} [-q | -v] [-p] [-i | -s] [-rp | -rf] > offense_report" >> Dev:StdErr
  221.     exit 2
  222. end
  223.  
  224. #
  225. # Create the file list.
  226. #
  227.  
  228. set files_to_search "`Files {recursiveFiles} -t TEXT -f -o -s "{TargetDir}"`"
  229.  
  230. #
  231. # Search the listed files and generate the report
  232. #
  233.  
  234. if "{byPattern}"
  235.     for pattern in {{Patterns}}
  236.         if "{Progress}"
  237.             echo
  238.             echo "#==================================================================================="
  239.             echo "#••••• Pattern {pattern}..."
  240.             echo
  241.             for file in {files_to_search}
  242.                 if "{pattern}" =~ /∂/≈∂//
  243.                     search {Pattern} "{file}" {searchOpts}
  244.                 else
  245.                     search "{Pattern}" "{file}" {searchOpts}
  246.                 end
  247.                 if "{status}"
  248.                     echo "# Pattern '{Pattern}' not found in file '{file}'"
  249.                 end
  250.             end
  251.         else
  252.             if "{pattern}" =~ /∂/≈∂//
  253.                 search {Pattern} {searchOpts} {files_to_search}
  254.             else
  255.                 search "{Pattern}" {searchOpts} {files_to_search}
  256.             end
  257.         end
  258.     end
  259. else
  260.     for file in {files_to_search}
  261.         if "{Progress}"
  262.             echo
  263.             echo "#==================================================================================="
  264.             echo "#••••• File {File}..."
  265.             echo
  266.         end
  267.         for pattern in {{Patterns}}
  268.             if "{pattern}" =~ /∂/≈∂//
  269.                 search {Pattern} "{file}" {searchOpts}
  270.             else
  271.                 search "{Pattern}" "{file}" {searchOpts}
  272.             end
  273.             if "{status}" && "{Progress}"
  274.                 echo "# Pattern '{Pattern}' not found"
  275.             end
  276.         end
  277.     end
  278. end
  279.